import {
  Button,
  List,
  Modal,
  Grid,
  DatePickerView,
  Toast,
  InputItem,
} from 'antd-mobile';
import { Divider, Alert } from 'antd';
import moment from 'moment';
import React, { useState } from 'react';
import { connect, useRequest, history } from 'umi';
import { PlusCircleFilled } from '@ant-design/icons';
import BackIcon from '@/components/BackIcon';
import { useEffect } from 'react';
import { postFetch } from '@/utils/doFetch';
import { useMemo } from 'react';
import PublicList from '@/components/PublicList';
import { addresource } from '@/services/factory';
import styles from './index.less';

let Add = ({ global: { userinfo, companies } }) => {
  let [submit, setsubmit] = useState({
    license_copy_num: {
      text: '执照副本数量',
      value: 0,
    },
    license_original_num: {
      text: '执照正本数量',
      value: 0,
    },
    id_card: {
      text: '身份证号码',
      value: null,
      type: 'text',
    },
    official_seal_num: {
      text: '公章数量',
      value: 0,
    },
    invoice_seal_num: {
      text: '发票章数量',
      value: 0,
    },
    financial_seal_num: {
      text: '财务章数量',
      value: 0,
    },
    private_seal_num: {
      text: '私章数量',
      value: 0,
    },
    revenue_invoice_num: {
      text: '收入发票数量',
      value: 0,
    },
    cost_invoice_num: {
      text: '成本发票',
      value: 0,
    },
    remark: {
      text: '备注',
      value: null,
      type: 'text',
    },
  });

  const { run, loading } = useRequest(postFetch, {
    manual: true,
    formatResult: (res) => res,
    onSuccess: (result, params) => {
      if (result.code == 0) {
        Toast.success('操作成功');
        history.go(-1);
      }
    },
  });

  return (
    <div style={{ height: '100%' }}>
      <BackIcon></BackIcon>
      <p
        style={{
          padding: '22px 16px 2px 16px',
          width: '100%',
          textAlign: 'center',
          fontSize: 16,
        }}
        className="oneline"
      >
        {companies?.name}
      </p>
      <Divider style={{ marginTop: 0, marginBottom: 0 }}></Divider>
      {Object.keys(submit).map((it, i) => {
        return (
          <InputItem
            key={i}
            type={submit[it].type || 'number'}
            placeholder={'请输入' + submit[it].text}
            moneyKeyboardAlign="right"
            clear
            value={submit[it].value}
            onChange={(v) => {
              setsubmit({
                ...submit,
                [it]: {
                  ...submit[it],
                  value: v,
                },
              });
            }}
          >
            {submit[it].text}
          </InputItem>
        );
      })}
      <Button
        loading={loading}
        style={{ color: '#fff', borderRadius: 0, marginTop: 12 }}
        type="primary"
        onClick={() => {
          let postdata = {};

          for (let i in submit) {
            postdata[i] = submit[i].value;
          }

          run({
            url: '/wbn-api/wechat/customers/resource',
            params: { ...postdata, company_id: companies?.id },
          });
        }}
      >
        添加
      </Button>
    </div>
  );
};

export default connect(({ global, loading }) => ({
  global,
  loading,
}))(Add);
